package com.yx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.dao.HouseMapper;
import com.yx.dao.OwnerMapper;
import com.yx.dao.PropertyInfoMapper;
import com.yx.dao.PropertyTypeMapper;
import com.yx.model.House;
import com.yx.model.Owner;
import com.yx.model.PropertyInfo;
import com.yx.model.PropertyType;
import com.yx.service.IPropertyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class PropertyInfoServiceImpl extends ServiceImpl<PropertyInfoMapper, PropertyInfo> implements IPropertyInfoService {

    @Autowired
    private PropertyInfoMapper propertyInfoDao;

    @Autowired
    private PropertyTypeMapper propertyTypeMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private OwnerMapper ownerMapper;

    @Override
    public PageInfo<PropertyInfo> findPropertyInfoAll(int page, int pagesise,
                                                      PropertyInfo propertyInfo) {
        PageHelper.startPage(page, pagesise);
        List<PropertyInfo> list = propertyInfoDao.queryListAll(propertyInfo);
        return new PageInfo(list);
    }

    @Override
    public void deleteInfoByHouIdAndTime(Integer houId, Date endTime) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(endTime);
        dateString = dateString.substring(0, 9);
        propertyInfoDao.deleteByHouIdAndTime(houId, dateString);
    }

    @Override
    public IPage<PropertyInfo> findListByPage(Integer page, Integer pageCount) {
        IPage<PropertyInfo> wherePage = new Page<>(page, pageCount);
        PropertyInfo where = new PropertyInfo();

        return baseMapper.selectPage(wherePage, Wrappers.query(where));
    }

    @Override
    public int add(PropertyInfo propertyInfo) {
        return baseMapper.insert(propertyInfo);
    }

    @Override
    public int delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateData(PropertyInfo propertyInfo) {
        return baseMapper.updateById(propertyInfo);
    }

    @Override
    public PropertyInfo findById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 分页条件查询
     *
     * @param propertyInfos
     * @param propertyInfo
     * @return
     */
    @Override
    public IPage<PropertyInfo> findPropertyInfoAll(Page<PropertyInfo> propertyInfos, PropertyInfo propertyInfo) {
        // 封装条件
        Integer status = propertyInfo.getStatus();
        QueryWrapper<PropertyInfo> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }

        // 查询结果
        Page<PropertyInfo> infoPage = propertyInfoDao.selectPage(propertyInfos, wrapper);

        // 进行数据封装
        List<PropertyInfo> pageRecords = infoPage.getRecords();
        pageRecords = pageRecords.stream().map(propertyInfo1 -> {
            // 查询出房屋信息
            Integer houseId = propertyInfo1.getHouseId();
            House house = houseMapper.selectById(houseId);
            propertyInfo1.setHouse(house);

            // 查询费用类型
            Integer typeId = propertyInfo1.getTypeId();
            PropertyType propertyType = propertyTypeMapper.selectById(typeId);
            propertyInfo1.setType(propertyType);

            // 查询拥有者信息
            QueryWrapper<Owner> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("house_id", houseId);
            Owner owner = ownerMapper.selectOne(queryWrapper);
            propertyInfo1.setOwner(owner);
            // 返回数据
            return propertyInfo1;
        }).collect(Collectors.toList());


        // 进行条件封装
        House house = propertyInfo.getHouse();
        String numbers;
        if (!StringUtils.isEmpty(house)) {
            numbers = house.getNumbers();
            if (!StringUtils.isEmpty(numbers)) {
                pageRecords = pageRecords.stream().filter(propertyInfo1 -> {
                    House info1House = propertyInfo1.getHouse();

                    String houseNumbers = info1House.getNumbers();
                    if (houseNumbers.equals(numbers)) {
                        return true;
                    } else {
                        return false;
                    }
                }).collect(Collectors.toList());

            }
        }

        // 重新封装数据
        infoPage.setRecords(pageRecords);
        return infoPage;
    }
}
